package com.enmotech.kafkatest.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * com.enmotech.kafkatest.controller
 *
 * @author syf
 * @create 2022-08-22-15:24
 * @Description kafka-test
 */
@Component
public class Payload {

    private String payload;

    //字段属性,使用包装类，则可以赋null值
    private Double longitude;
    private Double latitude;
    private Double temperature;
    private Double humidity;
    private int Timestamp;
    private String stringtime;
    private String randomString;

    //记录数据量
    //private int conut=0;

    //时间戳与date格式转换
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //生成随机数和随机字符串
    Random random = new Random();
    //RandomStringUtils randomStringUtils = new RandomStringUtils();
    char[] house = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', '我', '你', '他', '是', '否', '对', '错', '好', '坏', '快', '慢'};

    public String getPayload(int target,int count) {
        //生成随机数
        longitude = random.nextDouble()*180;
        latitude = random.nextDouble()*180;
        temperature = Math.abs(random.nextDouble()*43);
        humidity = Math.abs(random.nextDouble()*100);
        Timestamp = Math.abs((int) random.nextLong());
        stringtime = sdf.format(Timestamp);
        randomString = RandomStringUtils.random(20,house);
        int temp = random.nextInt(10);
        switch (temp){
            case 2:
                longitude = null;
                break;
            case 3:
                latitude = null;
                break;
            case 4:
                temperature = null;
                break;
            case 5:
                humidity = null;
                break;
        }
        payload = "\"payload\":{\"id\":" + count + ",\"longitude\":" + longitude + ",\"latitude\":" + latitude + ",\"temperature\":" + temperature + ",\"humidity\":" + humidity + ",\"tabletime\":" + Timestamp + ",\"stringtime\":\"" + stringtime + "\",\"randomstring\":";
        //使用target控制randomString出现null的概率
        if (temp<target){
            randomString = null;
            payload = payload+randomString+"}}";
        }else {
            payload = payload + "\"" + randomString + "\"}}";
        }
        return payload;
    }
}
